#include "PrecompiledHeader.h"
#include "IAsyncCalc.h"

/*--------------------------------------------------------------------------*/

IAsyncCalc::IAsyncCalc(void)
{
	this->m_description = NULL;
}

/*--------------------------------------------------------------------------*/

IAsyncCalc::~IAsyncCalc(void)
{
}

/*--------------------------------------------------------------------------*/

void IAsyncCalc::StartAsync(uint arg)
{
	AssertLogC(this->m_description != NULL, "Already working!");

	if (!this->m_description)
	{
		this->m_description = new AsyncTask();

		this->m_description->m_arg = arg;
		this->m_description->m_task = this;
		this->m_description->m_thread = SDL_CreateThread(AsyncCalc, (void*) this->m_description);
	}
}

/*--------------------------------------------------------------------------*/

void IAsyncCalc::OnAsyncFinished(uint arg)
{
	AssertLogC(this->m_description == NULL, "AsyncTask NULL?");

	if (this->m_description)
	{
		delete this->m_description;
		this->m_description = NULL;

		this->OnAsyncReady(arg);
	}
}

/*--------------------------------------------------------------------------*/

int AsyncCalc(void* data)
{
	AsyncTask* calc = (AsyncTask*) data;

	while (g_Calc->LimitAsyncTask())
	{
		g_Sdl->Delay(1);
	}

	calc->m_task->OnAsyccCalc(calc->m_arg);
	calc->m_task->OnAsyncFinished(calc->m_arg);

	return 0;
}

/*--------------------------------------------------------------------------*/